package com.roads.web.controller.system;

import com.roads.common.config.MasterConfig;
import com.roads.common.constant.Constants;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.domain.entity.SysUser;
import com.roads.common.utils.SecurityUtils;
import com.roads.common.utils.StringUtils;
import com.roads.engineering.constant.PaymentConstant;
import com.roads.engineering.domain.summary.SummaryIpc;
import com.roads.engineering.service.ISummaryService;
import com.roads.project.domain.ProInstitution;
import com.roads.project.domain.ProPeriod;
import com.roads.project.service.IProBoqService;
import com.roads.project.service.IProInstitutionService;
import com.roads.project.service.IProPeriodService;
import com.roads.project.service.IProProjectService;
import com.roads.site.service.ISiteLabourStaffService;
import com.roads.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 首页
 *
 * @author nhp
 */
@RestController
public class SysIndexController {
    /**
     * 系统基础配置
     */
    @Autowired
    private MasterConfig masterConfig;

    @Autowired
    private IProProjectService projectService;

    @Autowired
    private IProInstitutionService institutionService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IProBoqService boqService;

    @Autowired
    private IProPeriodService periodService;

    @Autowired
    private ISummaryService summaryService;

    @Autowired
    private ISiteLabourStaffService labourStaffService;

    /**
     * 访问首页，提示语
     */
    @RequestMapping("/")
    public String index() {
        return StringUtils.format("欢迎使用{}信息化管理平台，当前版本：v{}，请通过前端地址访问。", masterConfig.getName(), masterConfig.getVersion());
    }

    /**
     * 加载项目数据,根据用户的管理标段获取项目相关金额
     *
     * @return 结果
     */
    @RequestMapping("/loadProjectTotalData/{projectId}")
    public AjaxResult loadProjectTotalData(@PathVariable("projectId") Integer projectId) {
        AjaxResult ajaxResult = new AjaxResult();
        String userName = SecurityUtils.getUsername();
        boolean isAdmin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        SysUser user = userService.selectUserByUserName(userName);
        // 合同总金额
        BigDecimal contractTotal = projectService.getContractTotal(projectId, user, isAdmin);
        ajaxResult.put("contractTotal", contractTotal);
        // 本期完成金额,累计完成金额
        BigDecimal currentTotal = BigDecimal.valueOf(0);
        BigDecimal currentEndTotal = BigDecimal.valueOf(0);
        List<ProInstitution> bids = null;
        if (isAdmin) {
            bids = institutionService.selectBidListByProjectId(projectId);
        } else {
            bids = institutionService.selectBidListByUserAndProjectId(projectId, userName);
        }
        for (ProInstitution bid : bids) {
            ProPeriod period = periodService.getLastLockPeriod(bid.getInnerCode());
            if (StringUtils.isNotNull(period)) {
                String summaryUser = summaryService.getSummaryLastUser(bid.getInnerCode(), period.getPeriodName());
                if (StringUtils.isNotEmpty(summaryUser)) {
                    SummaryIpc ipc = summaryService.getItemByBidAndPeriodAndItemCode(bid.getInnerCode(), period.getPeriodName(), summaryUser, PaymentConstant.ZQZFZS_QDXJ);
                    if (StringUtils.isNotNull(ipc)) {
                        BigDecimal summaryCurrent = ipc.getCurrentPeriodTotal() == null ? BigDecimal.valueOf(0) : ipc.getCurrentPeriodTotal();
                        BigDecimal summaryCurrentEnd = ipc.getCurrentEndPeriodTotal() == null ? BigDecimal.valueOf(0) : ipc.getCurrentEndPeriodTotal();
                        currentTotal = currentTotal.add(summaryCurrent);
                        currentEndTotal = currentEndTotal.add(summaryCurrentEnd);
                    }
                }
            }
        }
        ajaxResult.put("currentTotal", currentTotal.setScale(0, BigDecimal.ROUND_HALF_UP));
        ajaxResult.put("currentEndTotal", currentEndTotal.setScale(0, BigDecimal.ROUND_HALF_UP));
        // 变更金额
        BigDecimal changeTotal = boqService.getChangeTotal(projectId, user, isAdmin);
        ajaxResult.put("changeTotal", changeTotal);
        return ajaxResult;
    }

    /**
     * 加载项目数据,根据用户的管理标段获取项目相关人员
     *
     * @return 结果
     */
    @RequestMapping("/loadProjectPersonData/{projectId}")
    public AjaxResult loadProjectPersonData(@PathVariable("projectId") Integer projectId) {
        AjaxResult ajaxResult = new AjaxResult();
        Integer yz = 0, yz1 = 0, yz2 = 0;
        Integer jl = 0, jl1 = 0, jl2 = 0;
        Integer cbs = 0, cbs1 = 0, cbs2 = 0;
        Integer lwg = 0, lwg1 = 0, lwg2 = 0;
        lwg = labourStaffService.getStaffCountByProjectId(projectId, "");
        lwg1 = labourStaffService.getStaffCountByProjectId(projectId, "2");
        lwg2 = labourStaffService.getStaffCountByProjectId(projectId, "3");
        ajaxResult.put("yz", yz);
        ajaxResult.put("yz1", yz1);
        ajaxResult.put("yz2", yz2);
        ajaxResult.put("jl", jl);
        ajaxResult.put("jl1", jl1);
        ajaxResult.put("jl2", jl2);
        ajaxResult.put("cbs", cbs);
        ajaxResult.put("cbs1", cbs1);
        ajaxResult.put("cbs2", cbs2);
        ajaxResult.put("lwg", lwg);
        ajaxResult.put("lwg1", lwg1);
        ajaxResult.put("lwg2", lwg2);
        return ajaxResult;
    }

}
